package com.yifi.system.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.DBHelper;
import com.yifi.common.base.BaseController;
import com.yifi.common.model.MenuModel;
import com.yifi.common.utils.DateUtils;
import com.yifi.common.utils.JWTUtils;
import com.yifi.common.utils.StringUtils;
import com.yifi.common.utils.ToolSession;
import com.yifi.system.domain.MisUser;
import com.yifi.system.service.MenuService;
import com.yifi.system.service.MisUserService;

@Path(value = "/tomain", viewPath = "/")
public class MainController extends BaseController {

	@Inject
	private MenuService menuService;

	//
	@Clear
	public void tokenLogin() {
		String gettoken = getPara("token", "");
		String username = JWTUtils.getUserByCol(gettoken, "user_num");
		MisUserService misUserService = new MisUserService();
		String sql = "select t1.*,(select cname from mis_org where ccode=t1.cdept) as orgname,(select db_type from org_info where ccode=t1.c_pro) as db_type "
				+ " from mis_user t1 where user_num=?";
		MisUser misUser = (MisUser) misUserService.getDao().findFirst(sql, username);
		if (misUser != null) {
			misUserService.setSession(this, misUser, true);
		}
		redirect("/tomain");
	}

	public void index() {
		String isweb = getPara(0, "");
		if (StringUtils.isNotEmpty(isweb)) {
			redirect("/");
		} else {
			MisUser misUser = ToolSession.getUser(this);

			loginDesc("登录成功");
			if (misUser != null) {
				setAttr("user", misUser);
			}

			// 用户权限菜单
			List<MenuModel> listMenu = menuService.getParent(menuService.getMenu(misUser.getCcode()));
			setAttr("menus", listMenu);

			// List<MenuModel> listMenuP =
			// menuService.getParentMenu(menuService.getMenu(misUser.getCcode()));
			// setAttr("menuP", listMenuP);
			String ccode = misUser.getCcode();
			//设置产权
			set("c_pro", misUser.getC_pro());

			String sql = "select cproname from org_info where ccode='" + misUser.getC_pro() + "'";
			String cproname = DBHelper.GetString(sql);
			setAttr("cproname", cproname);

			render("main.html");
		}
	}

	public void main() {
		String isweb = getPara(0, "");
		if (StringUtils.isNotEmpty(isweb)) {
			redirect("/");
		} else {
			String c_pro = ToolSession.getUser(this).getC_pro();

			render("default.html");
		}
	}

	// 工作流任务列表
	public void getTaskList() {
		String cuser = ToolSession.getUser(this).getCcode();
		String ctype = getPara("ctype", "");
		String ctitle = getPara("ctitle", "");

		String sqlSelect = " select t1.*,t2.cname as dept,t3.cname as cuser  ";

		String sqlExcept = " FROM task_list t1,MIS_ORG t2,mis_user t3 "
				+ " WHERE t1.ctask_dept=t2.ccode and t3.ccode=t1.create_by and (t1.CTASK_FLAG = '0' or t1.CTASK_FLAG = '1') "
				+ " AND  t1.CTASK_PROCESS like '%;" + cuser + ";%'    ";

		sqlExcept += " ORDER BY t1.CTASK_START_DATE DESC   ";

		renderJson(getDataTable(sqlSelect, sqlExcept));
	}
}
